layui.use(['form', 'table'], function () {
    let $ = layui.jquery, form = layui.form, table = layui.table;
    //表格渲染
    table.render({
        elem: '#currentTableId',
        url: '/api/student/get/list',
        toolbar: '#toolbarDemo',
        defaultToolbar: ['filter', 'exports', 'print', {
            title: '提示',
            layEvent: 'LAYTABLE_TIPS',
            icon: 'layui-icon-tips'
        }],
        cols: [
            [
                {type: "checkbox", width: 50, fixed: "left"},
                {field: 'stuCode', width: 120, title: '学号', sort: true},
                {field: 'name', width: 100, title: '姓名', font: 'bold'},
                {field: 'sex', width: 100, title: '性别', templet:function(e){
                        return e.sex == "1" ? "男" : "女";
                    }},
                {field: 'age', width: 100, title: '年龄', sort: true, templet:function(e){
                        return e.age&&e.age!=null&&e.age!=0?e.age + "岁":'';
                    }},
                {field: 'classInfo',width: 200, title: '班级', sort: true, templet:function(e){
                        return stuClassFormat(e);
                    }},
                {field: 'teacherName', width: 100, title: '班主任', templet:function(e){
                        return e.stuClass.teacherName;
                    }},
                {field: 'stuStatus', width: 100, title: '在校状态', templet :function (e){
                        return stuStatusFormat(e.stuStatus);
                    }},
                {field: 'createDate', width: 150, title: '创建时间', templet :function (e){
                        return createDateFormat(e.createDate);
                    }},
                {title: '操作', minWidth: 120, templet: '#currentTableBar', fixed: "right", align: "center"}
            ]
        ],
        limits: [10, 15, 20, 25, 50, 100],
        limit: 10,
        page: true
    });

    //搜索提交
    searchEvent();

    //工具栏事件（添加、删除）
    toolbarEvent();

    //表格复选框选择
    checkboxEvent();

    //单行编辑删除事件
    rowEditDelEvent()

});


//工具栏事件（添加、删除）
function toolbarEvent() {
    layui.table.on('toolbar(currentTableFilter)', function(obj){
        switch(obj.event){
            case 'add':
                addEvent();
                break;
            case 'delete':
                checkboxDelEvent();
                break;
        };
    });
};

//搜索事件
function searchEvent() {
    layui.form.on('submit(data-search-btn)', function (data) {
        //执行搜索重载
        reload(data)
        return false;
    });
}

//添加事件
function addEvent() {
    let index = layer.open({
        title: '添加学生信息',
        type: 2,
        shade: 0.2,
        maxmin:true,
        shadeClose: true,
        area: ['80%', '80%'],
        content: '/page/student/edit.html',
    });
    return false;
}

//多选删除事件
function checkboxDelEvent() {
    let checkStatus = layui.table.checkStatus('currentTableId') , data = checkStatus.data;
    let param = data.map(e => {return e.id});
    if(param.length > 0){
        layer.confirm('确定删除？', function (index) {
            layer.close(index);
            postAjax("/api/student/delete", param, function (res) {
                layer.msg(res.msg);
                reload(data);
            })
        });
    }else {
        layer.msg('请选择要删除的数据');
    }
}

//复选框选择事件
function checkboxEvent() {
    layui.table.on('checkbox(currentTableFilter)', function (obj) {
        // console.log(obj)
    });
}

//单行编辑删除事件
function rowEditDelEvent() {
    layui.table.on('tool(currentTableFilter)', function (obj) {
        let data = obj.data;
        if (obj.event === 'edit') {
            let index = layer.open({
                title: '编辑学生信息',
                type: 2,
                shade: 0.2,
                maxmin:true,
                shadeClose: true,
                area: ['80%', '80%'],
                content: '/page/student/edit.html?id='+data.id+'&classId='+data.stuClass.id,
            });
            return false;
        } else if (obj.event === 'delete') {
            layer.confirm('确定删除？', function (index) {
                layer.close(index);
                let param = new Array(obj.data.id);
                postAjax("/api/student/delete", param, function (res) {
                    layer.msg(res.msg);
                    reload();
                })
            });
        }
    });
}

//表格重载
function reload(data) {
    if(!data || data == ''){
        data = layui.table.checkStatus('currentTableId').data
    }
    layui.table.reload('currentTableId', {
        page: {
            curr: 1
        },
        where: data.field
    }, 'data');
}

//学生状态式化
function stuStatusFormat(e){
    if(e == '1'){
        return '正常';
    }else if(e == '2'){
        return '事假';
    }else if(e == '3'){
        return '病假';
    }else if(e == '4'){
        return '休学';
    }else if(e == '5'){
        return '开除';
    }else if(e == '6'){
        return '毕业';
    }else {
        return '未知';
    }
}

function stuClassFormat(e) {
    var className = e.stuClass.className;
    if(e.stuClass.grade && e.stuClass.stuClass){
        className += "（" + e.stuClass.grade + "年级" + e.stuClass.stuClass + "班" + "）";
    }
    return className;
}